﻿using System.Collections.Generic;

namespace Glue.Internals
{
    internal class MapState:INestedObjectCreator
    {
        private enum StateEnum
        {
            AutomagicalState,
            StrictState
        }

        private StateEnum State { get; set; }

        private readonly INestedObjectCreator callWhenInAutomagicalState;
        private readonly INestedObjectCreator callWhenInStrictState;

        public MapState(INestedObjectCreator callWhenInAutomagicalState, INestedObjectCreator callWhenInStrictState)
        {
            this.callWhenInAutomagicalState = callWhenInAutomagicalState;
            this.callWhenInStrictState = callWhenInStrictState;
            State = StateEnum.AutomagicalState;
        }

        public bool TryCreateAllObjectsInPath(object theObject, IEnumerable<IObjectMemberAccessor> memberAccessors)
        {
            if (StateEnum.AutomagicalState == State)
                return callWhenInAutomagicalState.TryCreateAllObjectsInPath(theObject, memberAccessors);
            if (StateEnum.StrictState== State)
                return callWhenInStrictState.TryCreateAllObjectsInPath(theObject, memberAccessors);
            throw new GlueException("System error: Strange MapState ("+State+"). Contact the developers of Glue");
        }

        internal class DoNothingState:INestedObjectCreator
        {
            public bool TryCreateAllObjectsInPath(object theObject, IEnumerable<IObjectMemberAccessor> memberAccessors)
            {
                return false;
            }
        }

        public void SetAutomagicalState()
        {
            State = StateEnum.AutomagicalState;
        }

        public void SetStrictState()
        {
            State = StateEnum.StrictState;
        }
    }
}
